#include "BulletManager.h" 
#include "cocostudio\CocoStudio.h"


BulletManager::BulletManager(){

}

BulletManager::~BulletManager(){

}

bool BulletManager::init(){
	this->scheduleUpdate();
	return true;
}

void BulletManager::update(float dt){
	if (m_player->isDead()){
		//this->unscheduleUpdate();
		return;
	}
	Vector<Bullet*> bulletList;
	for (auto bullet : this->getChildren()){
		bulletList.pushBack((Bullet*)(bullet));
	}
	Vector<Enemy*> enemyList = m_enemyManager->getEnemyList();
	for (auto bullet : bulletList){
		if (bullet->getIsPlayer()){
			bullet->getBox(bullet->getPosition().x, bullet->getPosition().y);
			for (auto enemy : enemyList){
				if (bullet->isInRect(enemy->getBoundingBox())){
					enemy->hurt(bullet->getAtk());
					bullet->boom();
				}
			}	
		}
		else{
			if (bullet->isInRect(m_player->getBoundingBox())){
				m_player->hurt(bullet->getAtk());
				bullet->boom();
			}
		}
	}
}

void BulletManager::bindEnemyManager(EnemyManager* enemyManager){
	m_enemyManager = enemyManager;
}

void BulletManager::bindPlayer(Player* player){
	m_player = player;
}
